package com.mall.server.service.pms;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.autoconfig.exception.BusinessException;
import com.mall.dubbo.api.PmsBrandApi;
import com.mall.model.domain.PmsBrand;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.ErrorResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PmsBrandService {

    @DubboReference
    private PmsBrandApi pmsBrandApi;

    public void saveBrand(PmsBrand pmsBrand) {
        PmsBrand brand= pmsBrandApi.findBrand(pmsBrand.getName());
        if (brand!=null){
            return ;
        }
        pmsBrandApi.saveBrand(pmsBrand);
    }

    public void deleteBrandIds(Long[] ids) {
        pmsBrandApi.deleteBrandIds(ids);
    }

    public void deleteBrandId(Long id) {
        pmsBrandApi.deleteBrandId(id);
    }

    public CommonPage<PmsBrand> queryList(String keyword, Integer pageNum, Integer pageSize) {

        Page<PmsBrand> iPage = pmsBrandApi.queryList(keyword, pageNum, pageSize);
        CommonPage<PmsBrand> commonPage = new CommonPage<>(iPage);
        return commonPage;
    }

    public List<PmsBrand> listAll() {
        return pmsBrandApi.listAll();
    }

    public void updateFactoryStatus(Integer factoryStatus, Long[] ids) {
        List<PmsBrand> pmsBrands=pmsBrandApi.findBrandByids(ids);
        if (pmsBrands.size()==0){
            return;
        }
        for (PmsBrand pmsBrand : pmsBrands) {
            pmsBrand.setFactoryStatus(factoryStatus);
            pmsBrandApi.saveBrand(pmsBrand);
        }
    }

    public void updateShowStatus(Integer showStatus, Long[] ids) {
        List<PmsBrand> pmsBrands=pmsBrandApi.findBrandByids(ids);
        if (pmsBrands.size()==0){
            return;
        }
        for (PmsBrand pmsBrand : pmsBrands) {
            pmsBrand.setShowStatus(showStatus);
            pmsBrandApi.saveBrand(pmsBrand);
        }
    }

    public void updateBrand(Integer id, PmsBrand brand) {
        PmsBrand pmsBrand= pmsBrandApi.findBrandByid(Convert.toLong(id));
        brand.setId(Convert.toLong(id));
        if (pmsBrand==null){
            pmsBrandApi.saveBrand(pmsBrand);
        }
        pmsBrandApi.updateBrand(brand);

    }

    public PmsBrand findBrandByid(Integer id) {

        return pmsBrandApi.findBrandByid(Convert.toLong(id));
    }
}
